package com.sta.staff.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sta.staff.bean.Result;
import com.sta.staff.mapper.UsersMapper;
import com.sta.staff.pojo.Users;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UsersService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private BCryptPasswordEncoder encoder;
    //根据工号查询用户
    public Users findByUsersName(String username){
        QueryWrapper<Users> wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        Users users = usersMapper.selectOne(wrapper);
        return users;
    }
    //根据工号查询用户权限
    public Boolean findByUsersName1(String username){
        QueryWrapper<Users> wrapper = new QueryWrapper();
        wrapper.eq("username",username);
        Users users = usersMapper.selectOne(wrapper);
        return users.isStatus() ;
    }
    // 分页查询所有用户
    public Page<Users> findPage(int page, int size){
        Page selectPage = usersMapper.selectPage(new Page(page, size), null);
        return selectPage;
    }
    //根据id将需要修改的用户信息返回到修改页面
    public Users findByUId(Integer uid){
        return usersMapper.selectById(uid);
    }
    //根据id修改用户信息
    public void update(Users users){
        usersMapper.updateById(users);
    }

    //根据条件模糊查询
    public Page<Users> findByLike(String search,int page, int size){
        QueryWrapper<Users> queryWrapper = new QueryWrapper();
        queryWrapper.like("username",search).or().like("name",search)
                .or().eq("sex",search).or().eq("department",search)
                .or().eq("phoneNum",search);
        Page selectPage = usersMapper.selectPage(new Page(page, size), queryWrapper);
        return selectPage;
    }

    //开除员工
    public void delete(Integer uid){
        usersMapper.deleteById(uid);
    }

    //查询个人信息
    public Users findMyself(String username){
        QueryWrapper<Users> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        Users users1 = usersMapper.selectOne(queryWrapper);
        return users1;
    }

    //修改个人信息
    public Users findMyEdit(Integer uid){
        Users users1 = usersMapper.selectById(uid);
        return users1;
    }

    //保存修改的个人信息
    public Result UpdateMyself(Users users){

        if (users.getName()==null || users.getName().length()<1){
            return new Result(false,"姓名不能为空");
        }
        if (users.getPhoneNum()==null || users.getPhoneNum().length()<1){
            return new Result(false,"手机号不能为空");
        }


        if (users.getAddress()==null || users.getAddress().length()<1){
            return new Result(false,"地址不能为空");
        }
        if (users.getSex()==null || users.getSex().length()<1){
            return new Result(false,"性别不能为空");
        }

        //旧电话
        String oldPhoneNum=usersMapper.selectById(users.getUid()).getPhoneNum();
        //新电话
        String newPhoneNum=users.getPhoneNum();
        if (newPhoneNum==null || newPhoneNum.length()<1){
            return new Result(false,"密码不能为空");
        }
        //如果新电话不等于旧电话
        if (!oldPhoneNum.equals(newPhoneNum)){

            QueryWrapper<Users> queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("phoneNum",users.getPhoneNum());
            List<Users> usersList1 = usersMapper.selectList(queryWrapper1);
            if (usersList1.size()>0){
                return new Result(false,"手机号已存在");
            }
        }

        //旧密码
        String oldPassword=usersMapper.selectById(users.getUid()).getPassword();
        //新密码
        String newPassword=users.getPassword();
        if (newPassword==null || newPassword.length()<1){
            return new Result(false,"密码不能为空");
        }
        //如果密码不等于旧密码，对新密码加密
        if (!oldPassword.equals(newPassword)){
            users.setPassword(encoder.encode(users.getPassword()));
        }

        usersMapper.updateById(users);
        return new Result(true,"修改成功");
    }

}
